package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.mapper.UserMapper;
import cn.tedu._05weibo.pojo.dto.UserLoginDTO;
import cn.tedu._05weibo.pojo.dto.UserRegDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
@Api(tags = "01.用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private  UserMapper userMapper;
    //1.注册功能
    @ApiOperation(value = "注册功能")
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO){
        /*
         *1.接收客户端传递的数据
         *2.校验数据的合法性
         *3.确定用户名是否被占用
         * 3.1.如果被占用，返回2
         * 3.2.如果没被占用，返回1
         */
        log.debug("userRegDTO ="+userRegDTO);
        String username = userRegDTO.getUsername();
        UserVO userVO = userMapper.selectByUsername(username);
        log.debug("userVO="+userVO);
        if (userVO!=null){
            return new JsonResult(StatusCode.USERNAME_EXIST);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.ok();
    }
    //2.登录功能
    @ApiOperation(value = "登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO,@ApiIgnore HttpSession session){
        /**
         * 1.接收客户端传递的数据
         * 2.校验用户名[Select]
         * 3.校验密码
         */
        System.out.println("userLoginDTO = " + userLoginDTO);
        String username = userLoginDTO.getUsername();
        //SELECT
        UserVO userVO = userMapper.selectByUsername(username);
        if (userVO==null){//用户名错误
               return new JsonResult(StatusCode.USERNAME_ERROR);
        }
        //校验密码：用户提交的密码和数据库中查询到的密码
        if (userVO.getPassword().equals(userLoginDTO.getPassword())){
            session.setAttribute("user",userVO);
            return  JsonResult.ok();
        }
        return new JsonResult(StatusCode.PASSWORD_ERROR);//用户名或密码错误
    }
    //3.获取当前登录用户信息
    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserVO userVO =  (UserVO)session.getAttribute("user");
        return  JsonResult.ok(userVO);
    }
    //4.退出登录
    @ApiOperation(value = "注销功能")
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }

}





























